package com.scproject.scbef.controller.study.schoolnews;

import com.scproject.annotations.LoginVerifyToken;
import com.scproject.annotations.PassToken;
import com.scproject.dto.ResponseDto;
import com.scproject.scbef.pojo.SchoolNews;
import com.scproject.scbef.service.study.schoolnews.SchoolNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * @author LNL
 * @date 2021/5/20 21:27
 */
@RestController
@Api(tags = "校园新闻模块")
@RequestMapping("/news")
public class SchoolNewsController {

    @Autowired
    private SchoolNewsService schoolNewsService;

    @PassToken
    @GetMapping("/schoolNews")
    @ApiOperation("获取所有校园新闻")
    public ResponseDto getAllSchoolNews() {
        ResponseDto responseDto = schoolNewsService.getAllSchoolNews();
        return responseDto;
    }

    /**
     * 删除校园新闻
     * @return
     */
    @LoginVerifyToken
    @PostMapping("/delete/{id}")
    @ApiOperation("删除某个校园新闻")
    public ResponseDto deleteNews(@ApiParam("校园新闻id") @PathVariable("id") String id) {
        ResponseDto responseDto = schoolNewsService.deleteNewsById(id);
        return responseDto;
    }

    /**
     * 增加校园新闻，所需要的字段
     * title、content、publishTime、imgPath
     * @return
     */
    @LoginVerifyToken
    @PostMapping("/insert")
    @ApiOperation("增加校园新闻")
    public ResponseDto insertSchoolNews(MultipartHttpServletRequest request) {
        ResponseDto responseDto = schoolNewsService.insertNews(request);
        return responseDto;
    }

    /**
     * 修改校园新闻
     * id、title、content、publishTime、img
     * @return
     */
    @PostMapping("/update")
    @LoginVerifyToken
    @ApiOperation("修改校园新闻")
    public ResponseDto updateSchoolNews(@RequestBody SchoolNews schoolNews, HttpServletRequest request) {
        ResponseDto responseDto = schoolNewsService.updateSchoolNews(schoolNews,request);
        return responseDto;
    }


}
